package com.sun.service.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.sun.common.core.model.dto.DataDTO;
import com.sun.common.core.model.dto.SimplePageVO;
import com.sun.common.core.model.query.PageQuery;
import com.sun.service.dto.TmVhcVehicleMovementReportPointDto;
import com.sun.service.dto.TmVhcVehicleMovementReportPointExcelExportDto;
import com.sun.service.dto.TmVhcVehicleMovementReportPointQuery;
import com.sun.service.service.TmVhcVehicleMovementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/tmVhcVehicleMovement")
@Api(tags = {"车辆跟踪信息-TmVhcVehicleMovementController"})
public class TmVhcVehicleMovementController {

    @Resource
    public TmVhcVehicleMovementService iTmVhcVehicleMovementService;

    /**
     * 整车过点列表查询分页
     *
     * @param tmVhcVehicleMovementReportPointQuery
     * @return
     */
    @ApiOperation(value = "整车过点列表查询分页", notes = "整车过点列表查询分页", protocols = "http,https", httpMethod = "POST")
    @PostMapping(value = "/reportPoint/query", produces = {MediaType.APPLICATION_JSON_VALUE})
    public SimplePageVO<DataDTO<List<TmVhcVehicleMovementReportPointDto>>> queryReportPoint(@Valid PageQuery pageQuery, @RequestBody TmVhcVehicleMovementReportPointQuery tmVhcVehicleMovementReportPointQuery) {
        return new SimplePageVO<>(iTmVhcVehicleMovementService.queryReportPoint(pageQuery, tmVhcVehicleMovementReportPointQuery));
    }

    /**
     * 整车过点查询报表结果导出
     *
     * @param
     * @return
     */
    @ApiOperation(value = "整车过点查询报表结果导出", notes = "整车过点查询报表结果导出", protocols = "http,https", httpMethod = "POST")
    @PostMapping(value = "/reportPoint/export", produces = {MediaType.APPLICATION_JSON_VALUE})
    public void exportBDCSortByVin(@RequestBody TmVhcVehicleMovementReportPointQuery param, HttpServletResponse response) throws IOException {
        // 获取导出数据
        List<TmVhcVehicleMovementReportPointExcelExportDto> exports = iTmVhcVehicleMovementService.exportReportPoint(param);

        // 导出相关设置
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String excelName = URLEncoder.encode("reportVhcVehiclePoint.xlsx", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelName);
        // 调用EasyExcel将数据写入excel文件并导出
        EasyExcel.write(response.getOutputStream(), TmVhcVehicleMovementReportPointExcelExportDto.class)
                // 自适应宽度，但是这个不是特别精确
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet(1, "Sheet1")
                .doWrite(exports);
    }

}
